package com.kingdee.pda.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.WindowManager;

import com.kingdee.pda.R;
import com.kingdee.pda.callback.HavePermissionCallback;
import com.kingdee.pda.util.PermissionUtil;


/**
 * 启动界面
 */
public class LaunchActivity extends BaseActivity {

    // 延迟跳转时间
    private static final int DELAY_TIME = 1500;
    // 跳转到登录界面
    private static final int HANDLER_WHAT_TO_LOGIN = 1;

    private static final int PERMISSION_REQUEST_CODE_WRITE_EXTERNAL_STORAGE = 100;
    private String[] permissions = new String[]{
            Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
//            , Manifest.permission.SYSTEM_ALERT_WINDOW    // 悬浮窗权限android M以后就无法动态获取了，只能通过引导用户自己点击，我们用不上这个直接删了
    };

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case HANDLER_WHAT_TO_LOGIN:
                    getWindow().setFlags(
                            WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
                            WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
                    LoginActivity.actionStart(LaunchActivity.this);
                    finish();
                    break;
            }
            return true;
        }
    });


    @Override
    protected void onActivityCreate(@Nullable Bundle savedInstanceState) {
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_launch);
        // 避免从桌面启动程序后，会重新实例化入口类的activity
        if (replaceLoad()) return;
    }

    @Override
    protected void initView() {
//        ImageView ivLaunch = (ImageView) findViewById(R.id.iv_launch);
//        ivLaunch.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initData() {
        PermissionUtil.checkPermissionsByActivity(this, permissions, PERMISSION_REQUEST_CODE_WRITE_EXTERNAL_STORAGE, new HavePermissionCallback() {
            @Override
            public void onHavePermissionCallback(String permission, int requestCode) {
                mHandler.sendEmptyMessageDelayed(HANDLER_WHAT_TO_LOGIN, DELAY_TIME);
            }
        });
    }

    private boolean replaceLoad() {
        if (!this.isTaskRoot()) { // 判断当前activity是不是所在任务栈的根
            Intent intent = getIntent();
            if (intent != null) {
                String action = intent.getAction();
                if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(action)) {
                    getWindow().setFlags(
                            WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
                            WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
                    finish();
                    return true;
                }
            }
        }
        return false;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE_WRITE_EXTERNAL_STORAGE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //用户同意授权
                    mHandler.sendEmptyMessageDelayed(HANDLER_WHAT_TO_LOGIN, DELAY_TIME);
                } else {
                    //用户拒绝授权
                    finish();
                }
                break;
        }
    }

    @Override
    protected void onNetworkAvailable() {

    }

    @Override
    protected void onNetworkError() {

    }

}
